package com.mmlc.meimei.lichengkongjian.Adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.mmlc.businessutils.OrderListUtils;
import com.mmlc.businessutils.OrderShowUtils;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.OrderButtonBean;
import com.mmlc.meimei.lichengkongjian.customview.MuchButtonGroup;
import com.mmlc.meimei.lichengkongjian.customview.MuchOrderItemGroup;
import com.mmlc.meimei.lichengkongjian.mine.ActivityOrderDetail;
import com.mmlc.network.responsebean.OrderListResponseBean;

import java.util.List;


public class AllOrderAdapter extends MyBaseAdapter {

    ViewGroup rootView;
    List<OrderListResponseBean.DataBean> item;
    public OrderListUtils d;
    public BaseParentActivity activity;
    OrderShowUtils orderShowUtils;

    public AllOrderAdapter(OrderShowUtils orderShowUtils, ViewGroup viewGroup, List<OrderListResponseBean.DataBean> item, BaseParentActivity activity) {
        this.rootView = viewGroup;
        this.item = item;
        this.activity = activity;
        this.orderShowUtils = orderShowUtils;
    }

    @Override
    public int getCount() {
        return item.size();
    }

    @Override
    public OrderListResponseBean.DataBean getItem(int i) {
        return item.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void removeItem(int position) {
        item.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        final OrderListResponseBean.DataBean bean = item.get(position);
//        ViewHolder vh = null;
        if (view == null) {
//            vh = new ViewHolder();
            view = App.layoutInflater.inflate(R.layout.adapter_order_much3, viewGroup, false);
        }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activity, ActivityOrderDetail.class);
                    intent.putExtra("orderId", item.get(position).getId());
                    if (orderShowUtils.object instanceof Fragment) {
                        Fragment fragment = (Fragment) orderShowUtils.object;
                        fragment.startActivityForResult(intent, 1);
                    } else if (orderShowUtils.object instanceof Activity) {
                        Activity fragment = (Activity) orderShowUtils.object;
                        fragment.startActivityForResult(intent, 1);
                    }
                }
            });


            TextView tv_orderNumber = (TextView) view.findViewById(R.id.tv_orderNumber);
            tv_orderNumber.setText("订单编号: " + bean.getId());

            ImageView iv_orderType = (ImageView) view.findViewById(R.id.iv_ordertype);
            if (Integer.parseInt(bean.getType()) > 0) {
//                if (bean.getType().equals("1")) {
//
//                } else {
//                    iv_orderType.setVisibility(View.GONE);
//                }
//                if (bean.getType().equals("2")) {
//
//                } else {
//                    iv_orderType.setVisibility(View.GONE);
//                }
//                if (bean.getType().equals("3")) {
//
//                } else {
//
//                }

                switch ( bean.getType()){
                    case "1":
                        iv_orderType.setImageDrawable(App.context.getResources().getDrawable(R.drawable.order_store));
                        iv_orderType.setVisibility(View.VISIBLE);
                        break;
                    case "2":
                        iv_orderType.setImageDrawable(App.context.getResources().getDrawable(R.drawable.order_push));
                        iv_orderType.setVisibility(View.VISIBLE);
                        break;
                    case "3":
                        iv_orderType.setImageDrawable(App.context.getResources().getDrawable(R.drawable.ic_fast));
                        iv_orderType.setVisibility(View.VISIBLE);
                        break;
                    default:
                        iv_orderType.setVisibility(View.GONE);
                        break;
                }

            } else {
                iv_orderType.setVisibility(View.GONE);
            }

//            TextView tv_orderFrom = (TextView) view.findViewById(R.id.tv_orderFrom);
//            tv_orderFrom.setText(bean.getStore_name());

//            RelativeLayout rl_orderFrom = (RelativeLayout) view.findViewById(R.id.rl_orderFrom);
//            if (TextUtils.isEmpty(bean.getStore_name())) {
//                rl_orderFrom.setVisibility(View.GONE);
//            } else {
//                rl_orderFrom.setVisibility(View.VISIBLE);
//
//            }
            LinearLayout ll_push = (LinearLayout) view.findViewById(R.id.ll_push);
            if (!TextUtils.isEmpty(bean.getPush_msg())) {
                TextView tv_reason = (TextView) view.findViewById(R.id.tv_reason);
                tv_reason.setText(bean.getPush_msg());
                ll_push.setVisibility(View.VISIBLE);
            }else{
                ll_push.setVisibility(View.GONE);
            }


            TextView tv_orderState = (TextView) view.findViewById(R.id.tv_orderState);
            tv_orderState.setText(bean.getStatus_msg());

            TextView tv_allShopCount = (TextView) view.findViewById(R.id.tv_allShopCount);
            tv_allShopCount.setText("产品总价: ¥" + bean.getOrder_price());

            MuchOrderItemGroup mg_itemGroup = (MuchOrderItemGroup) view.findViewById(R.id.mg_itemGroup);
            mg_itemGroup.addItem(bean.getOrder_goods());
      /*  mg_itemGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, ActivityOrderDetail.class);
                intent.putExtra("orderId",bean.getId());
                activity.startActivity(intent);
            }
        });*/

            MuchButtonGroup mg_buttonGroup = (MuchButtonGroup) view.findViewById(R.id.mg_buttonGroup);
            mg_buttonGroup.addButton(OrderListUtils.getOper(bean.getOper()));
            mg_buttonGroup.setMuchButtonClickedListener(new MuchButtonGroup.MuchButtonClickedListener() {
                @Override
                public void clicked(View v, int type) {
                    if (d == null) {
                        d = new OrderListUtils();
                    }
                    d.setUpdateUI(new OrderListUtils.UpdateUI() {
                        @Override
                        public void updateUI(int position, int type) {
                            switch (type) {
                                case OrderListUtils.UpdateUI.REMOVE_ITEM:
                                    removeItem(position);
                                    break;
                                case OrderListUtils.UpdateUI.REFRESH:
                                    orderShowUtils.pullUtils.currPageIndex = 0;
                                    orderShowUtils.isNeedTransparent = true;
                                    orderShowUtils.isClearAdapterList = true;
                                    orderShowUtils.requestDataForList();
                                    break;
                            }
                        }
                    });
                    d.doSomething(getOrderBean(v, type));
                }

                private OrderButtonBean getOrderBean(View buttonView, int type) {
                    OrderButtonBean buttonBean = new OrderButtonBean();
                    buttonBean.rootView = rootView;
                    buttonBean.activity = activity;
                    buttonBean.itemPositon = position;
                    buttonBean.orderId = bean.getId();
                    buttonBean.buttonType = type;
                    buttonBean.payType = bean.getPay_code();
                    buttonBean.buttonView = buttonView;
                    buttonBean.saleAfterFlag = true;
                    buttonBean.from = OrderButtonBean.IGNORE;
                    buttonBean.orderType = Integer.valueOf(bean.getType());
                    buttonBean.suborder = bean.getSuborder();
                    buttonBean.context = activity;
                    return buttonBean;
                }
            });
            return view;
        }



    @Override
    public void deleteItem ( int position){
        item.remove(position);
        notifyDataSetChanged();
    }

}

